package net.xrkw.server.module.client.controller.admin.softcategory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import net.xrkw.server.framework.common.pojo.PageParam;
import net.xrkw.server.framework.common.pojo.PageResult;
import net.xrkw.server.framework.common.pojo.CommonResult;
import net.xrkw.server.framework.common.util.object.BeanUtils;
import static net.xrkw.server.framework.common.pojo.CommonResult.success;

import net.xrkw.server.framework.excel.core.util.ExcelUtils;

import net.xrkw.server.framework.apilog.core.annotation.ApiAccessLog;
import static net.xrkw.server.framework.apilog.core.enums.OperateTypeEnum.*;

import net.xrkw.server.module.client.controller.admin.softcategory.vo.*;
import net.xrkw.server.module.client.dal.dataobject.softcategory.SoftCategoryDO;
import net.xrkw.server.module.client.service.softcategory.SoftCategoryService;

@Tag(name = "管理后台 - 软件分类")
@RestController
@RequestMapping("/client/soft-category")
@Validated
public class SoftCategoryController {

    @Resource
    private SoftCategoryService softCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建软件分类")
    @PreAuthorize("@ss.hasPermission('client:soft-category:create')")
    public CommonResult<Long> createSoftCategory(@Valid @RequestBody SoftCategorySaveReqVO createReqVO) {
        return success(softCategoryService.createSoftCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新软件分类")
    @PreAuthorize("@ss.hasPermission('client:soft-category:update')")
    public CommonResult<Boolean> updateSoftCategory(@Valid @RequestBody SoftCategorySaveReqVO updateReqVO) {
        softCategoryService.updateSoftCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除软件分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('client:soft-category:delete')")
    public CommonResult<Boolean> deleteSoftCategory(@RequestParam("id") Long id) {
        softCategoryService.deleteSoftCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除软件分类")
                @PreAuthorize("@ss.hasPermission('client:soft-category:delete')")
    public CommonResult<Boolean> deleteSoftCategoryList(@RequestParam("ids") List<Long> ids) {
        softCategoryService.deleteSoftCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得软件分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('client:soft-category:query')")
    public CommonResult<SoftCategoryRespVO> getSoftCategory(@RequestParam("id") Long id) {
        SoftCategoryDO softCategory = softCategoryService.getSoftCategory(id);
        return success(BeanUtils.toBean(softCategory, SoftCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得软件分类分页")
    @PreAuthorize("@ss.hasPermission('client:soft-category:query')")
    public CommonResult<PageResult<SoftCategoryRespVO>> getSoftCategoryPage(@Valid SoftCategoryPageReqVO pageReqVO) {
        PageResult<SoftCategoryDO> pageResult = softCategoryService.getSoftCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SoftCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出软件分类 Excel")
    @PreAuthorize("@ss.hasPermission('client:soft-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSoftCategoryExcel(@Valid SoftCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SoftCategoryDO> list = softCategoryService.getSoftCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "软件分类.xls", "数据", SoftCategoryRespVO.class,
                        BeanUtils.toBean(list, SoftCategoryRespVO.class));
    }

}